package com.linkvin.ec.hcs.service.mdm.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.linkvin.ec.hcs.dao.mapper.mdm.VBrandMapper;
import com.linkvin.ec.hcs.dao.model.mdm.VBrand;
import com.linkvin.ec.hcs.dao.model.mdm.bo.VModelBo;
import com.linkvin.ec.hcs.service.mdm.VBrandService;
import com.linkvin.ec.hcs.service.util.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class VBrandServiceImpl implements VBrandService{

    @Autowired
    private VBrandMapper vBrandMapper;

    @Override
    public List<VBrand> listAllBrand() {
        EntityWrapper<VBrand> ew = new EntityWrapper<>();
        ew.orderBy("brand_prefix").orderBy("id");
        List<VBrand> vBrands = vBrandMapper.selectList(ew);
        return vBrands;
    }

    @Override
    public VModelBo addBrand(VModelBo vModelBo) {
        //非空校验
        if (Strings.isNullOrEmpty(vModelBo.getBrandName().trim())) throw new BaseException("品牌名称不能为空");
        //判断是否重复
        EntityWrapper<VBrand> ew = new EntityWrapper<>();
        ew.where("brand_name={0}",vModelBo.getBrandName().trim());
        int count = vBrandMapper.selectCount(ew);
        if(count > 0){
            throw new BaseException("品牌名称重复");
        }
        VBrand vBrand = new VBrand();
        vBrand.setBrandName(vModelBo.getBrandName());
        vBrand.setBrandPrefix(vModelBo.getBrandPrefix());
        vBrand.setBrandNation(vModelBo.getBrandNation());
        vBrand.setComment(vModelBo.getBrandComment());
        count = vBrandMapper.insert(vBrand);
        if(count > 0){
            vModelBo = getModelBO(vModelBo.getBrandName());
        }
        return  vModelBo;
    }

    @Override
    public VModelBo updateBrand(VModelBo vModelBo) {
        //非空校验
        if (Strings.isNullOrEmpty(vModelBo.getBrandName().trim())) throw new BaseException("品牌名称不能为空");
        //判断是否重复
        EntityWrapper<VBrand> ew = new EntityWrapper<>();
        ew.where("id != {0}",vModelBo.getId()).and("brand_name={0}",vModelBo.getBrandName().trim());
        int count = vBrandMapper.selectCount(ew);
        if(count > 0){
            throw new BaseException("品牌名称重复");
        }
        VBrand vBrand = new VBrand();
        vBrand.setId(vModelBo.getBrandId());
        vBrand.setBrandName(vModelBo.getBrandName());
        vBrand.setBrandPrefix(vModelBo.getBrandPrefix());
        vBrand.setBrandNation(vModelBo.getBrandNation());
        vBrand.setComment(vModelBo.getBrandComment());
        count = vBrandMapper.updateById(vBrand);
        if(count > 0){
            vModelBo = getModelBO(vModelBo.getBrandName());
        }
        return  vModelBo;
    }

    @Override
    public int deleteBrand(List<Integer> vModelBoIds) {
        //TODO 需要判断是否已经使用
        return vBrandMapper.deleteBatchIds(vModelBoIds);
    }

    @Override
    public List<VModelBo> queryPageVBrand(Page<VModelBo> vModelBoPage) {
        List<VModelBo> listVehicleBo = vBrandMapper.queryPageVBrand(vModelBoPage,buildCondition(vModelBoPage.getCondition()));
        return  listVehicleBo;
    }

    private EntityWrapper<VModelBo> buildCondition(Map<String,Object> conditionBrand){
        EntityWrapper<VModelBo> ew = new EntityWrapper<>();
        if(!Objects.equals(conditionBrand,null)) {
            for (Map.Entry<String, Object> entry : conditionBrand.entrySet()) {
                if (entry.getKey().equals("name")) {
                    ew.like("brand_name", entry.getValue().toString());
                }
            }
        }
        return ew;
    }

    private VModelBo getModelBO(String brandName){
        VModelBo vModelBo = new VModelBo();
        List<VModelBo> listBrandBo= vBrandMapper.selectBrandByName(brandName);
        if(!CollectionUtils.isEmpty(listBrandBo)){
            vModelBo = listBrandBo.get(0);
        }
        return vModelBo;
    }
}
